<?php
/**
 * Create by PhpStorm
 * User: lijia
 * Data: 2020/8/10
 * Time: 10:52
 */

namespace app\api\business;


use app\admin\model\Config as ConfigModel;
use app\api\controller\UserCommon;
use think\Exception;
use think\facade\Db;

class RedEnvelopeBus extends UserCommon
{

    private $config;
    public function __construct(){
        parent::__construct();
        $this->config = ConfigModel::getCfgByModule("SYSTEM_CONFIG");
    }

    /**
     * 领取红包
     */
    public function redEnvelope($param)
    {
        $param['user_id'] = $this->user_id;
        $param['create_time'] = toDate(time());
        $param['money'] = $this->config['money'];
        $date = date("Y-m-d", time());
        $check = Db::name('red_envelopes')->where('user_id', $this->user_id)->where('create_time', '>', $date)->find();
        if ($check){
            throw new Exception('今天你已领取过红包了！');
        }
        $todayCount = Db::name('red_envelopes')->where('create_time', '>', $date)->count();
        if ($todayCount >= $this->config['number']){
            throw new Exception('今天的红包已被领完！');
        }
        // 启动事务
        Db::startTrans();
        try {
            $moneyLog = [
                'user_id' => $this->user_id,
                'money' => $this->config['money'],
                'type' => 5,
                'create_time' => toDate(time())
            ];
            Db::name('money_log')->insert($moneyLog);
            Db::name('red_envelopes')->insert($param);
            Db::name('user')->where('user_id',$this->user_id)->inc('money',$this->config['money'])->update();
            // 提交事务
            Db::commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 判断是否可以领取红包
     */
    public function checkEnvelope()
    {
        if ($this->config['site_status'] != 1){
            return ['status' => 0,'watch_time' => 0,'money' => 0];
        }
        $date = date("Y-m-d", time());
        $check = Db::name('red_envelopes')->where('user_id', $this->user_id)->where('create_time', '>', $date)->find();
        if ($check){
            return ['status' => 0,'watch_time' => 0,'money' => 0];
        }
        $todayCount = Db::name('red_envelopes')->where('create_time', '>', $date)->count();
        if ($todayCount >= $this->config['number']){
            return ['status' => 0,'watch_time' => 0,'money' => 0];
        }
        return ['status' => 1,'money' => $this->config['watch_time'],'watch_time' => $this->config['watch_time']];
    }


    /**
     * 问题列表
     */
    public function getQua()
    {
        $list = Db::name('qa')->field('id,title,qus,an')->select()->toArray();
        foreach ($list as $k => $v){
            $list[$k]['qus'] = json_decode($v['qus'], true);
        }
        return $list;
    }


    /**
     * 检查今日是否可竞猜答题
     */
    public function checkQa(){
        $date = date("Y-m-d", time());
        $check = Db::name('reward')->where('user_id', $this->user_id)->where('status', 1)->where('create_time', '>', $date)->find();
        if ($check){
            return ['status' => 0];
        }else{
            return ['status' => 1];
        }
    }


    /**
     * 答题 领取奖励
     */
    public function answer($param){
        $info = ConfigModel::getCfgByModule("UPLOAD_IMG");
        $param['user_id'] = $this->user_id;
        $param['money'] = $info['reward'];
        $param['create_time'] = toDate(time());
        Db::name('reward')->insert($param);
        $moneyLog = [
            'user_id' => $this->user_id,
            'money' => $info['reward'],
            'type' => 5,
            'create_time' => toDate(time())
        ];
        Db::name('money_log')->insert($moneyLog);
        if ($param['status'] == 1){
            Db::name('user')->where('user_id', $this->user_id)->inc('money', $info['reward'])->update();
            return ['reward' => $info['reward']];
        }
        return ['reward' => 0];
    }
}